#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "TuioPlayer/texttoosc.h"
#include "lo/lo.h"

#ifdef WITH_TIME
long long getTimeFromText(const char *nf_buff)
{
    // read time from OSC DUMP

    char nf_extract[100]={0};

    int nf_cpt;

    for(nf_cpt=0;(nf_buff[nf_cpt]!=' ');)
    {
        nf_extract[nf_cpt]=nf_buff[nf_cpt++];
    }

    nf_extract[nf_cpt]=0; // add '\0' char at end

    return atoll(nf_extract);
}
#endif

void text_to_osc(const char *nf_buff,char *nf_path, lo_message *nf_lo_m)
{
    char    nf_message[100]={0};
    char    nf_extract[100]={0};

    int nf_idx, nf_lim,nf_cpt=0;

    nf_lim=strlen(nf_buff);
    nf_idx=0;
    //extract path from buffer

#ifdef WITH_TIME
    // dummy extraction for time value at beginning of string

    while(nf_buff[nf_idx]!=' ')
    {
        nf_idx++;
    }
    nf_idx++;
#endif

    for(nf_cpt=0;((nf_idx<nf_lim) && (nf_buff[nf_idx]!=' '));)
    {
        nf_path[nf_cpt++]=nf_buff[nf_idx++];
    }

    nf_path[nf_cpt]=0; // add '\0' char at end
#ifdef VERBOSE
    printf("   path=");
    puts(nf_path);
#endif
    for(nf_idx++,nf_cpt=0;((nf_idx<nf_lim)&&(nf_buff[nf_idx]!=' '));)
    {
        nf_message[nf_cpt++]=nf_buff[nf_idx++];
    }

    nf_message[nf_cpt]=0;
#ifdef VERBOSE
    printf("   message=");
    puts(nf_message);
#endif
    lo_message_add_string(*nf_lo_m,nf_message);

    if(strcmp(nf_message,"source")==0)
    {
        for(nf_idx++,nf_cpt=0;((nf_idx<nf_lim)&&(nf_buff[nf_idx]!=' '));)
        {
            nf_extract[nf_cpt++]=nf_buff[nf_idx++];
        }
        nf_extract[nf_cpt]=0;
#ifdef VERBOSE
        printf("   source=");
        puts(nf_extract);
#endif
        lo_message_add_string(*nf_lo_m,nf_extract);
    }
    else if (strcmp(nf_message,"alive")==0) // alive frame contains only ints (session ids of alive Tuio containers
    {
#ifdef VERBOSE
        printf("   alive : ");
#endif
        while(nf_idx<nf_lim)
        {
            for(nf_idx++,nf_cpt=0;((nf_idx<nf_lim)&&(nf_buff[nf_idx]!=' '));)
            {
                nf_extract[nf_cpt++]=nf_buff[nf_idx++];
            }
            nf_extract[nf_cpt]=0;

            if (nf_cpt) // check if string is empty before updating lo_message
            {
#ifdef VERBOSE
                printf("%d ",atoi(nf_extract));
#endif
                lo_message_add_int32(*nf_lo_m,atoi(nf_extract));
            }
        }
#ifdef VERBOSE
       printf("\n");
#endif
    }
    else if (strcmp(nf_message,"set")==0)
    {
#ifdef VERBOSE
        printf("   set : ");
#endif
        // step 1 : the first parameter is a session id : int

        for(nf_idx++,nf_cpt=0;((nf_idx<nf_lim)&&(nf_buff[nf_idx]!=' '));)
        {
            nf_extract[nf_cpt++]=nf_buff[nf_idx++];
        }
        nf_extract[nf_cpt]=0;
#ifdef VERBOSE
        printf(" session id = %d ",atoi(nf_extract));
#endif
        lo_message_add_int32(*nf_lo_m,atoi(nf_extract));

        if(strstr(nf_path,"obj")) // a tuio object has a class ID : int
        {
            for(nf_idx++,nf_cpt=0;((nf_idx<nf_lim)&&(nf_buff[nf_idx]!=' '));)
            {
                nf_extract[nf_cpt++]=nf_buff[nf_idx++];
            }
            nf_extract[nf_cpt]=0;
#ifdef VERBOSE
            printf(" TUIO object has session id = %d",atoi(nf_extract));
#endif
            lo_message_add_int32(*nf_lo_m,atoi(nf_extract));
        }

        while(nf_idx<nf_lim) // remaining values are of float type
        {
            for(nf_idx++,nf_cpt=0;((nf_idx<nf_lim)&&(nf_buff[nf_idx]!=' '));)
            {
                nf_extract[nf_cpt++]=nf_buff[nf_idx++];
            }
            nf_extract[nf_cpt]=0;
#ifdef VERBOSE
            printf("%f ",atof(nf_extract));
#endif
            lo_message_add_float(*nf_lo_m,atof(nf_extract));
        }
#ifdef VERBOSE
    printf("\n");
#endif

    }
    else if (strcmp(nf_message,"fseq")==0) // fseq frame has only an int arg
    {
        for(nf_idx++,nf_cpt=0;((nf_idx<nf_lim)&&(nf_buff[nf_idx]!=' '));)
        {
            nf_extract[nf_cpt++]=nf_buff[nf_idx++];
        }
        nf_extract[nf_cpt]=0;
#ifdef VERBOSE
        printf("   fseq : %d\n",atoi(nf_extract));
#endif
        lo_message_add_int32(*nf_lo_m,atoi(nf_extract));
    }

    return;
}
